/*******************************************************************************
 * Package: com.hngtrust.iir.base.service.impl
 * Type:    LogServiceImpl
 * Date:    2021-8-16 20:19
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.base.service.impl;

import com.hngtrust.framework.id.service.MyIdGenerator;
import com.hngtrust.framework.page.PageResult;
import com.hngtrust.framework.page.service.PageService;
import com.hngtrust.framework.security.entity.LoginUser;
import com.hngtrust.framework.security.util.SecurityUtils;
import com.hngtrust.iir.base.controller.vo.ListOnlineRespVO;
import com.hngtrust.iir.base.controller.vo.ListOptLogRespVO;
import com.hngtrust.iir.base.controller.vo.OptLogReqVO;
import com.hngtrust.iir.base.entity.Dict;
import com.hngtrust.iir.base.entity.OptLog;
import com.hngtrust.iir.base.service.DictService;
import com.hngtrust.iir.base.service.LogService;
import com.hngtrust.iir.base.service.OptLogService;
import com.hngtrust.iir.constant.Constant;
import com.hngtrust.iir.dto.ReqDTO;
import com.hngtrust.iir.user.dto.ListOptLogReqVO;
import com.hngtrust.iir.user.entity.User;
import com.tiger.security.service.SecurityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 基础数据操作实现
 * @author Chentianyong
 * @date 2021-8-16 20:19
 */
@Service
public class LogServiceImpl implements LogService {
    
    @Autowired
    private OptLogService optLogService;
    
    @Autowired
    private MyIdGenerator myIdGenerator;
    
    @Autowired
    private DictService dictService;
    
    @Autowired
    private SecurityService securityService;
    
    /**
     * 分页服务
     */
    @Autowired
    private PageService pageService;
    
    @Override
    public void saveOptLog(OptLogReqVO vo) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        User user = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        OptLog req = new OptLog();
        req.setId(myIdGenerator.nextId());
        req.setUserId(user.getId());
        req.setUserCode(user.getUserCode());
        req.setUserName(user.getName());
        //ip
        req.setIp(loginUser.getLoginIp());
        req.setOptModule(vo.getOptModule());
        req.setOptContent(vo.getOptContent());
        req.setOptType(vo.getOptType());
        req.setStatus(vo.getStatus());
        req.setCreateTime(new Date());
    
        optLogService.add(req);
    }
    
    @Override
    public PageResult<ListOptLogRespVO> listOptLog(ReqDTO<ListOptLogReqVO> vo) {
        ListOptLogReqVO logVo = vo.getContent();
        PageResult<OptLog> page = pageService.page(vo.getPageParam(), () -> optLogService.queryOptLog(logVo));
    
        //数据处理
        List<ListOptLogRespVO> voList = new ArrayList<>();
        Map<String, Dict> moduleMap = dictService.queryDict("SYS_MODULE");
        Map<String, Dict> typeMap = dictService.queryDict("OPT_TYPE");
        Map<String, Dict> sfMap = dictService.queryDict("SUCCESS_FAIL");
        for(OptLog obj:page.getRows()){
            ListOptLogRespVO optLog = new ListOptLogRespVO();
            BeanUtils.copyProperties(obj, optLog);
            
            if(null != optLog.getOptModule() && optLog.getOptModule() > 0  && null != moduleMap && null != moduleMap.get(optLog.getOptModule().toString())){
                optLog.setOptModuleName(moduleMap.get(optLog.getOptModule().toString()).getBusiName());
            }
            if(null != optLog.getOptType() && optLog.getOptType() > 0  && null != typeMap && null != typeMap.get(optLog.getOptType().toString())){
                optLog.setOptTypeName(typeMap.get(optLog.getOptType().toString()).getBusiName());
            }
            if(null != optLog.getStatus() && optLog.getStatus() > 0  && null != sfMap && null != sfMap.get(optLog.getStatus().toString())){
                optLog.setStatusName(sfMap.get(optLog.getStatus().toString()).getBusiName());
            }
            
            voList.add(optLog);
        }
        
        PageResult<ListOptLogRespVO> resp = new PageResult<>();
        resp.setRows(voList);
        resp.setPage(page.getPage());
    
        return resp;
    }
    
    @Override
    public PageResult<ListOptLogRespVO> listLoginLog(ReqDTO<ListOptLogReqVO> vo) {
        ListOptLogReqVO logVo = vo.getContent();
        PageResult<OptLog> page = pageService.page(vo.getPageParam(), () -> optLogService.queryLoginLog(logVo));
    
        //数据处理
        List<ListOptLogRespVO> voList = new ArrayList<>();
        Map<String, Dict> moduleMap = dictService.queryDict("SYS_MODULE");
        Map<String, Dict> typeMap = dictService.queryDict("OPT_TYPE");
        Map<String, Dict> sfMap = dictService.queryDict("SUCCESS_FAIL");
        List<ListOnlineRespVO> onlineList = this.listOnline();
        for(OptLog obj:page.getRows()){
            ListOptLogRespVO optLog = new ListOptLogRespVO();
            BeanUtils.copyProperties(obj, optLog);
        
            if(null != optLog.getOptModule() && optLog.getOptModule() > 0  && null != moduleMap && null != moduleMap.get(optLog.getOptModule().toString())){
                optLog.setOptModuleName(moduleMap.get(optLog.getOptModule().toString()).getBusiName());
            }
            if(null != optLog.getOptType() && optLog.getOptType() > 0  && null != typeMap && null != typeMap.get(optLog.getOptType().toString())){
                optLog.setOptTypeName(typeMap.get(optLog.getOptType().toString()).getBusiName());
            }
            if(null != optLog.getStatus() && optLog.getStatus() > 0  && null != sfMap && null != sfMap.get(optLog.getStatus().toString())){
                optLog.setStatusName(sfMap.get(optLog.getStatus().toString()).getBusiName());
            }
            optLog.setLoginStatusName("离线");
            for(ListOnlineRespVO online:onlineList){
                if(online.getId().equals(optLog.getUserId())){
                    optLog.setLoginStatusName("在线");
                }
            }
            
            voList.add(optLog);
        }
    
        PageResult<ListOptLogRespVO> resp = new PageResult<>();
        resp.setRows(voList);
        resp.setPage(page.getPage());
    
        return resp;
    }
    
    @Override
    public List<ListOnlineRespVO> listOnline() {
        List<ListOnlineRespVO> resp = new ArrayList<>();
        List<LoginUser> list = securityService.getAllOnlineUsers();
        for(LoginUser loginUser : list){
            User user = (User) loginUser.getAdditionalInfo();
            ListOnlineRespVO online = new ListOnlineRespVO();
            online.setId(Long.valueOf(user.getId()));
            online.setName(user.getName());
            online.setUserCode(user.getUserCode());
            online.setIp(loginUser.getLoginIp());
            online.setLoginTime(loginUser.getLoginTime());
            online.setStatus(Constant.LOG_OPTSTATE_SUCCESS);
            online.setStatusName("在线");
        
            resp.add(online);
        }
        //去重
        resp.sort(Comparator.comparing(ListOnlineRespVO::getLoginTime).reversed());
        for(int i = 0; i < resp.size()-1; i++) {
            for(int j = resp.size() - 1; j > i; j--) {
                if(resp.get(j).getId().equals(resp.get(i).getId())) {
                    resp.remove(j);
                }
            }
        }
        
        return resp;
    }
}
